package com.oocl.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import util.JsonUtil;

import com.oocl.pojo.Customer;
import com.oocl.service.CustomerService;
import com.oocl.serviceimpl.CustomerServiceImpl;

@Controller
public class CustomerController {
	
	@Resource(name="serviceImpl")
	private CustomerService customerService;


	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public CustomerController() {
		System.out.println("实例化controller");
	}
	
	@ResponseBody
	@RequestMapping(value="addCustomer",consumes="application/json")
	public String addCustomer(@RequestBody Customer customer){
		customerService.addCustomer(customer);
		
		String json = JsonUtil.toJson(customerService.findAll());
		return json;
	}
	
	@ResponseBody
	@RequestMapping(value="delCustomer")
	public String deleteCustomer(String id){
//		System.out.println(id);
		String flag = customerService.delCustoemr(id);
		return flag;

	}
	
	@ResponseBody
	@RequestMapping(value="findAll")
	public String findAll(){
		List<Customer> list = customerService.findAll();
		String json = JsonUtil.toJson(list);
		return json;
	}
	
	@ResponseBody
	@RequestMapping(value="updateCustomer",consumes="application/json")
	public Customer updateCustomer(@RequestBody Customer customer){
		customerService.updateCustomer(customer);
		return customer;	
	}
}
